//
//  NavigationViewController.m
//  KKMYForU
//
//  Created by 黄磊 on 13-11-22.
//  Copyright (c) 2013年 黄磊. All rights reserved.
//

#import "NavigationViewController.h"
#import "BackNavItem.h"

@implementation UIViewController (NavigationViewController)


- (NavigationViewController *)navController
{
    UIViewController *viewController = self.parentViewController;
    while (!(viewController == nil || [viewController isKindOfClass:[NavigationViewController class]]))
    {
        viewController = viewController.parentViewController;
    }
    
    return (NavigationViewController *)viewController;
}



@end

@interface NavigationViewController ()

@end

@implementation NavigationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
    self = [super initWithRootViewController:rootViewController];
    if (self)
    {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    
    if (__CUR_IOS_VERSION >= __IPHONE_7_0)
    {
        // This code will only compile on versions >= iOS 7.0
        self.edgesForExtendedLayout= UIRectEdgeTop | UIRectEdgeBottom;
        self.navigationBar.barTintColor = kAppActiveColor;
        
    }
    else if (__CUR_IOS_VERSION >= __IPHONE_6_0)
    {
        self.navigationBar.tintColor = kAppActiveColor;
    }

    NSDictionary *textAttr = [NSDictionary dictionaryWithObjectsAndKeys:
                              [UIColor whiteColor], TextAttributeTextColor,
                              [UIFont fontWithName:@"JXiHei" size:17], TextAttributeFont,
                              [UIFont boldSystemFontOfSize:17], TextAttributeFont,
                              [UIColor clearColor], UITextAttributeTextShadowColor,nil];
    self.navigationBar.titleTextAttributes = textAttr;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)showBackButtonWith:(UIViewController *)viewController
{
    UIBarButtonItem *leftItem = [self createButtonWith:self action:@selector(back)];
    [viewController.navigationItem setLeftBarButtonItem:leftItem animated:YES];
}

- (UIBarButtonItem *)showRightButtonWith:(UIViewController *)viewController title:(NSString*)title action:(SEL)action
{
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:viewController action:action forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(250, 5, 56, 30);
    UIColor *textColor = [UIColor colorWithRed:(CGFloat)77.0/255 green:(CGFloat)194.0/255 blue:(CGFloat)167.0/255 alpha:1];
    UIColor *highLightColor = [UIColor colorWithRed:(CGFloat)209/255 green:(CGFloat)239/255 blue:(CGFloat)231/255 alpha:1];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn.titleLabel setTextColor:textColor];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [btn setTitleColor:textColor forState:UIControlStateNormal];
    [btn setTitleColor:highLightColor forState:UIControlStateHighlighted];
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [viewController.navigationItem setRightBarButtonItem:rightItem];
    return rightItem;
}

- (UIBarButtonItem *)showRightButtonWith:(UIViewController *)viewController image:(UIImage *)image action:(SEL)action
{
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn addTarget:viewController action:action forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(270, 5, 30, 30);
    UIColor *textColor = [UIColor colorWithRed:(CGFloat)77.0/255 green:(CGFloat)194.0/255 blue:(CGFloat)167.0/255 alpha:1];
    UIColor *highLightColor = [UIColor colorWithRed:(CGFloat)209/255 green:(CGFloat)239/255 blue:(CGFloat)231/255 alpha:1];
    [btn setImage:image forState:UIControlStateNormal];
    [btn.titleLabel setTextColor:textColor];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [btn setTitleColor:textColor forState:UIControlStateNormal];
    [btn setTitleColor:highLightColor forState:UIControlStateHighlighted];
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [viewController.navigationItem setRightBarButtonItem:rightItem];
    return rightItem;
}


- (void)showBackButtonWith:(UIViewController *)viewController andAction:(SEL)action
{
    UIBarButtonItem *leftItem = [self createButtonWith:viewController action:action];
    [viewController.navigationItem setLeftBarButtonItem:leftItem animated:YES];
}

- (UIBarButtonItem *)createButtonWith:(id)target action:(SEL)action
{
    // 设置返回键
    BackNavItem *backNav = [[BackNavItem alloc] initWithTitle:@"   " type:UIBackNavDefault target:target action:action];
    return backNav;
}


- (void)back
{
    if (self.viewControllers.count > 1) {
        [self popViewControllerAnimated:YES];
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}


-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count == 1) {
        if ([[self.viewControllers objectAtIndex:0] isKindOfClass:[TabBarViewController class]]) {            [self setNavigationBarHidden:YES animated:NO];
        }
    }
    [super pushViewController:viewController animated:animated];
    [self setNavigationBarHidden:NO animated:YES];

}



//显示商品的搜索按钮
-(UITextField *)showGoodsSearch:(UIViewController *)viewController placeholder:(NSString *)placeholder withSearchAction:(SEL)action{
    UITextField *_searchBox=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 320,29)];
    _searchBox.borderStyle=UITextBorderStyleNone;
    _searchBox.layer.cornerRadius=2.0f;
    
    _searchBox.clearButtonMode=UITextFieldViewModeWhileEditing;
    _searchBox.delegate=(id<UITextFieldDelegate>)viewController;
    
    _searchBox.backgroundColor=[UIColor whiteColor];
    _searchBox.placeholder=placeholder;
    viewController.navigationItem.titleView=_searchBox;
    UIBarButtonItem *searchButton=[[UIBarButtonItem alloc]initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:viewController action:action];
    viewController.navigationItem.rightBarButtonItem=searchButton;
    
    return _searchBox;
}

//显示订单的搜索按钮
-(void)showOrderSearchIcon:(UIViewController *)viewController withSearchAction:(SEL)action{
    
    UIImage *searchIcon=[UIImage imageNamed:@"search_ico"];
    UIBarButtonItem *buttonItem=[[UIBarButtonItem alloc]initWithImage:searchIcon style:UIBarButtonItemStylePlain target:viewController action:action];
    
    viewController.navigationItem.rightBarButtonItem=buttonItem;
    
    return;
}

@end
